<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Followers;
use app\common\model\Member;
use app\job\Message;
use EasyWeChat\Factory;
use think\Exception;
use think\Log;
use think\Queue;


class Gzh extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';


    public function server()
    {
        $wxConfig = config('wechat_gzh');
        $app = Factory::officialAccount($wxConfig);
        $app->server->push(function ($message) {
            Log::info($message);
            switch ($message['MsgType']) {
                case 'event'://收到事件消息
                    $returnInfo = $this->eventHandler($message);
                    return $returnInfo;
                    break;
                case 'text':
                    Queue::push(Message::class,[
                        'member_id'=>80,
                        'template_id'=>config('wechat_gzh')['gzh_template']['consumption_notice'],
                        'pagepath'=>'pages/public/order/index',
                        'data'=>['thing2'=>'车辆普洗','amount5'=>'10.25','car_number8'=>'闽D1EN11','time9'=>'2023-09-26 17:26','character_string12'=>'202309261134']
                    ],'message');
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });
        $response = $app->server->serve();
        $response->send();
    }

    private function eventHandler($message)
    {
        $wxConfig = config('wechat_gzh');
        $app = Factory::officialAccount($wxConfig);
        switch ($message['Event']) {
            case 'subscribe': //关注
                $openid = $message['FromUserName']; //用户的openid
                $userService = $app->user;
                $user = $userService->get($openid);
                $info = Followers::where(['openid'=>$user['openid']])->find();
                if ($info){
                    $info->subscribe_time = $user['subscribe_time'];
                    $info->subscribe = 1;
                    $info->save();
                }else{
                    Followers::create(['openid'=>$user['openid'],'unionid'=>$user['unionid'] ?? '','subscribe_time'=>$user['subscribe_time'],'subscribe'=>1]);
                }
                //查询是否有关注
                $member = Member::where(['wx_openid'=>$user['openid']])->whereOr(['unionid'=>$user['unionid']])->find();
                if ($member){
                    $member->subscribe = 1;
                    $member->wx_openid = $user['openid'];
                    $member->unionid = $user['unionid'];
                    $member->save();
                }
//                $content = "同安洗车界王炸来了！免费洗车！！！\n洗车福利大放送！百万补贴等你拿！\n新用户登录即可享受价值30元洗车优惠券！\n生日当月可领取一张价值66元洗车优惠券！\n无门槛直接抵扣！数量有限！先到先得！\n《联合众鑫》平台所有合作门店均可使用！\n\n点击下方链接进入小程序领取！\n<a data-miniprogram-appid='wx4bd6b93f69f561d8' data-miniprogram-path='pages/client/index/index' href='https://xm.xmlhzx.cn/'>进入领取优惠</a>";
                $content = "同安洗车界王炸！洗车福利大放送！百万补贴等你拿！\n【联合众鑫】洗车微信小程序\n此次优惠力度（无门槛直接抵扣）：\n第一批30元*1张=30元/位用户\n第二批15元*2张=30元/位用户\n第三批10元*3张=30元/位用户\n第四批5元*6张=30元/位用户\n生日66元*1张=66元/位用户\n30*4+66=186元/位用户\n每个用户累计可领取186元的洗车优惠券，限量50000份\n共计186*50000=930万！！！\n联合众鑫洗车微信小程序所有合作商家均可使用！\n#联合众鑫 #联合众鑫同安洗车 #联合众鑫同安救援\n\n点击下方链接进入小程序领取!\n<a data-miniprogram-appid='wx4bd6b93f69f561d8' data-miniprogram-path='pages/client/index/index' href='https://xm.xmlhzx.cn/'>进入领取优惠</a>";
                return $content;
                break;
            case 'unsubscribe': //取消关注
                $openid = $message['FromUserName']; //用户的openid
                //查询是否有关注
                $member = Member::where(['wx_openid'=>$openid])->find();
                if ($member){
                    $member->subscribe = 0;
                    $member->save();
                }
                $info = Followers::where(['openid'=>$openid])->find();
                if ($info){
                    $info->subscribe = 0;
                    $info->save();
                }
                Log::info('取消关注:' . $openid);
                return '取关';
                break;
            case 'SCAN': //扫码
                break;
            case 'CLICK': //点击
                break;
            default:
                return '收到其它消息';
                break;
        }
    }


    public function ckserver()
    {
        $wxconfig_gzh = config('wechat_gzh');
        $app = Factory::officialAccount($wxconfig_gzh);
        $response = $app->server->serve();
        return $response->send();
    }

}
